/*   Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
 *
 *   This file is part of CDinFusion.
 *
 *   CDinFusion is free software: you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public 
 *   License in version 3 (LGPL3) as published by the Free Software
 *   Foundation version 3 of the License.
 *
 *   CDinFusion is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the Lesser General Public
 *   License along with CDinFusion; see the file COPYING. If not,
 *   write to the Free Software Foundation, 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
 *   
 *   If you have any questions, send an email to: megx@mpi-bremen.de.
 */
package org.cdinfusion.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.cdinfusion.data.CDElement;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class Check_Input_Controller extends AbstractController {
	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println("##### Check_Input_Controller #####");
		HttpSession session = request.getSession();
		String origin = (String) session.getAttribute("origin");
		List<CDElement> cdeLIST = (List<CDElement>) session
				.getAttribute("cdeLIST");
		ModelAndView mav = null;

		if (origin.equals("4_again")) {
			String button = request.getParameter("submit");
			if (button.contains("change")) {
				List<CDElement> new_cdeLIST = new ArrayList<CDElement>();
				String nStudy = "";
				String nEnv = "";
				CDElement cdelement = null;
				int num_s = -1;

				if (cdeLIST.size() == 1) {
					cdeLIST.get(0).setKeepheader(Boolean.parseBoolean(request.getParameter("KeepFASTAHeader0")));
				}
				
				if (cdeLIST.size() > 1) {
					for (int i = 0; i < cdeLIST.size(); i++) {
						nStudy = request.getParameter("StudyTypeDropDown" + i);
						nEnv = request.getParameter("EnvParamDropDown" + i);

						if (nStudy.equals("none") | nEnv.equals("none")) {
							mav = new ModelAndView("ErrorPage");
							mav.addObject("origin", origin);
							return mav;
						}

						num_s = Integer.parseInt(nStudy.substring(nStudy
								.indexOf(" ") + 1));

						CDElement currElem = cdeLIST.get(i);
						cdelement = new CDElement(
								currElem.getFirstSequenceID(),
								currElem.getLastSequenceID(),
								currElem.getNumberOfSequences(), cdeLIST.get(
										num_s).getStudy(), cdeLIST.get(num_s)
										.getEnv(), currElem.getCDFastaheader(),
								Boolean.parseBoolean(request
										.getParameter("KeepFASTAHeader" + i)));
						new_cdeLIST.add(cdelement);
					}
					session.setAttribute("cdeLIST", new_cdeLIST);
				}// end if(cdeLIST.size() > 1)

				mav = new ModelAndView("4_MAPPING");
			}// end if(button.contains("change"))

			if (button.contains("proceed")) {
				mav = new ModelAndView("5_CONFIRM");
			}
		}// end if(origin.equals("4_again"))

		if (origin.equals("4")) {
			mav = new ModelAndView("5_CONFIRM");
		}
		return mav;
	}
}